<?php
session_start ();
require_once ("../../commun/ouvre_base.php");

$_SESSION ['pathway'] = '../..';
$_SESSION ['pathway_js'] = '/clinique';
include_once ($_SESSION ['pathway'] . "/header.php");

if (isset ( $_GET ['action'] )) {
	$action = $_GET ['action'];
}
if (isset ( $_GET ['form'] )) {
	$form = $_GET ['form'];
}

/*
 * $liste_fichiersattaches = array();
 * //echo getcwd();
 * if(isset($_FILES['fichiersattaches']) AND $_FILES['fichiersattaches']['error'] == 0){
 * move_uploaded_file($_FILES['fichiersattaches']['tmp_name'], $_SESSION['pathway'].$_SESSION['pathway_js'].'/uploads/'. basename($_FILES['fichiersattaches']['name']));
 * chmod($_SESSION['pathway'].$_SESSION['pathway_js'].'/uploads/' . basename($_FILES['fichiersattaches']['name']), 0777);
 * $fichiersattaches .= basename($_FILES['fichiersattaches']['name']).";";
 * }
 *
 * if(isset($_POST['fichiersattachesoriginal_0'])){
 * $fichiersattaches .= $_POST['fichiersattachesoriginal_0'];
 * }
 */

if (isset ( $_POST ['date'] ) and $_POST ['date'] != '') {
	$date = $_POST ['date'];
} else {
	$date = '';
}
if (isset ( $_POST ['nip'] ) and $_POST ['nip'] != '') {
	$nip = $_POST ['nip'];
} else {
	$nip = '';
}
if (isset ( $_POST ['fiche_spatax_attachee'] ) and $_POST ['fiche_spatax_attachee'] != '') {
	$fiche_spatax_attachee = $_POST ['fiche_spatax_attachee'];
} else {
	$fiche_spatax_attachee = '';
}
if (isset ( $_POST ['user'] ) and $_POST ['user'] != '') {
	$user = strtoupper ( $_POST ['user'] );
} else {
	$user = '';
}
if (isset ( $_POST ['neurologist'] ) and $_POST ['neurologist'] != '') {
	$neurologist = strtoupper ( $_POST ['neurologist'] );
} else {
	$neurologist = '';
}
if (isset ( $_POST ['site'] ) and $_POST ['site'] != '') {
	$site = $_POST ['site'];
} else {
	$site = '';
}
if (isset ( $_POST ['sex'] ) and $_POST ['sex'] != '') {
	$sex = $_POST ['sex'];
} else {
	$sex = '';
}
if (isset ( $_POST ['birthdate'] ) and $_POST ['birthdate'] != '') {
	$birthdate = $_POST ['birthdate'];
} else {
	$birthdate = '';
}
if (isset ( $_POST ['location'] ) and $_POST ['location'] != '') {
	$location = $_POST ['location'];
} else {
	$location = '';
}
if (isset ( $_POST ['location_other'] ) and $_POST ['location_other'] != '') {
	$location_other = mysql_real_escape_string ( $_POST ['location_other'] );
} else {
	$location_other = '';
}
if (isset ( $_POST ['diagnostic'] ) and $_POST ['diagnostic'] != '') {
	$diagnostic = $_POST ['diagnostic'];
	if (is_array ( $_POST ['diagnostic'] )) {
		$diagnostic = implode ( ', ', $_POST ['diagnostic'] );
	}
} else {
	$diagnostic = '';
}
if (isset ( $_POST ['diagnostic_genetique'] ) and $_POST ['diagnostic_genetique'] != '') {
	$diagnostic_genetique = $_POST ['diagnostic_genetique'];
} else {
	$diagnostic_genetique = '';
}
if (isset ( $_POST ['diagnostic_other'] ) and $_POST ['diagnostic_other'] != '') {
	$diagnostic_other = mysql_real_escape_string ( $_POST ['diagnostic_other'] );
} else {
	$diagnostic_other = '';
}
if (isset ( $_POST ['diagnostic_genetique_specify'] ) and $_POST ['diagnostic_genetique_specify'] != '') {
	$diagnostic_genetique_specify = mysql_real_escape_string ( $_POST ['diagnostic_genetique_specify'] );
} else {
	$diagnostic_genetique_specify = '';
}
if (isset ( $_POST ['diagnostic_complet'] ) and $_POST ['diagnostic_complet'] != '') {
	$diagnostic_complet = mysql_real_escape_string ( $_POST ['diagnostic_complet'] );
} else {
	$diagnostic_complet = '';
}
if (isset ( $_POST ['mode'] ) and $_POST ['mode'] != '') {
	$mode = $_POST ['mode'];
	if (is_array ( $_POST ['diagnostic'] )) {
		$mode = implode ( ', ', $_POST ['mode'] );
	}
} else {
	$mode = '';
}
if (isset ( $_POST ['mode_other'] ) and $_POST ['mode_other'] != '') {
	$mode_other = mysql_real_escape_string ( $_POST ['mode_other'] );
} else {
	$mode_other = '';
}
if (isset ( $_POST ['nouveau_patient'] ) and $_POST ['nouveau_patient'] != '') {
	$nouveau_patient = $_POST ['nouveau_patient'];
} else {
	$nouveau_patient = '';
}
if (isset ( $_POST ['prise_en_charge'] ) and $_POST ['prise_en_charge'] != '') {
	$prise_en_charge = $_POST ['prise_en_charge'];
} else {
	$prise_en_charge = '';
}
if (isset ( $_POST ['contexte_prise_en_charge'] ) and $_POST ['contexte_prise_en_charge'] != '') {
	$contexte_prise_en_charge = $_POST ['contexte_prise_en_charge'];
} else {
	$contexte_prise_en_charge = '';
} // cas ou l'on vient du modif_form
if (isset ( $_POST ['orientation_personnalisee'] ) and $_POST ['orientation_personnalisee'] != '') {
	$orientation_personnalisee = $_POST ['orientation_personnalisee'];
	if (is_array ( $_POST ['orientation_personnalisee'] )) {
		$orientation_personnalisee = implode ( ', ', $_POST ['orientation_personnalisee'] );
	}
} else {
	$orientation_personnalisee = '';
}
if (isset ( $_POST ['orientation_personnalisee_other'] ) and $_POST ['orientation_personnalisee_other'] != '') {
	$orientation_personnalisee_other = mysql_real_escape_string ( $_POST ['orientation_personnalisee_other'] );
} else {
	$orientation_personnalisee_other = '';
}

if ($action == 'edit') {
	$update = "UPDATE `$db`.`fiche_activite` SET 
	`date` = '$date',
	`nip` = '$nip',
	`fiche_spatax_attachee` = '$fiche_spatax_attachee',
	`user` = '$user',
	`neurologist` = '$neurologist',
	`site` = '$site',
	`sex` = '$sex',
	`birthdate` = '$birthdate',
	`location` = '$location',
	`location_other` = '$location_other',
	`diagnostic` = '$diagnostic',
	`diagnostic_genetique` = '$diagnostic_genetique',
	`diagnostic_other` = '$diagnostic_other',
	`diagnostic_genetique_specify` = '$diagnostic_genetique_specify',
	`diagnostic_complet` = '$diagnostic_complet',
	`mode` = '$mode',
	`mode_other` = '$mode_other',
	`nouveau_patient` = '$nouveau_patient',
	`prise_en_charge` = '$prise_en_charge',
	`contexte_prise_en_charge` = '$contexte_prise_en_charge',
	`orientation_personnalisee` = '$orientation_personnalisee',
	`orientation_personnalisee_other` = '$orientation_personnalisee_other' 
	WHERE `fiche_activite`.`form` = '$form';";
	// echo $update;
	mysql_query ( $update ) or die ( "Can't modify form activite : " . mysql_error () );
	$_POST = array ();
} else {
	$recupereformmaxfa = "select MAX(`form`) FROM `fiche_activite`";
	$resultform = mysql_query ( $recupereformmaxfa ) or die ( "Impossible de r&eacute;cup&eacute;rer l'form de la table fiche_activite et tu veux pas me dire pourquoi ?" . mysql_error () );
	while ( $ligne = mysql_fetch_row ( $resultform ) ) {
		$form_fa = $ligne [0] + 1;
	}
	$insertfa = "INSERT INTO `$db`.`fiche_activite` (
	`form`, 
	`date`, 
	`nip`, 
	`fiche_spatax_attachee`, 
	`user`, 
	`neurologist`, 
	`site`, 
	`sex`, 
	`birthdate`, 
	`location`, 
	`location_other`, 
	`diagnostic`, 
	`diagnostic_genetique`, 
	`diagnostic_other`, 
	`diagnostic_genetique_specify`, 
	`diagnostic_complet`,
	`mode`, 
	`mode_other`, 
	`nouveau_patient`, 
	`prise_en_charge`, 
	`contexte_prise_en_charge`, 
	`orientation_personnalisee`,
	`orientation_personnalisee_other`
	) VALUES (
	'$form_fa',
	'$date',
	'$nip', 
	'$fiche_spatax_attachee',
	'$user',
	'$neurologist',
	'$site',
	'$sex',
	'$birthdate', 
	'$location', 
	'$location_other', 
	'$diagnostic', 
	'$diagnostic_genetique', 
	'$diagnostic_other', 
	'$diagnostic_genetique_specify', 
	'$diagnostic_complet',
	'$mode', 
	'$mode_other', 
	'$nouveau_patient', 
	'$prise_en_charge', 
	'$contexte_prise_en_charge', 
	'$orientation_personnalisee',
	'$orientation_personnalisee_other'
	);";
	mysql_query ( $insertfa ) or die ( "Impossible d'ins&eacute;rer donn&eacute;es dans table form activite : " . mysql_error () );
	echo $insertfa;
}

if (isset ( $_POST ['submitprinc'] ) and $_POST ['submitprinc'] == 'upload') {
	$_POST = array ();
	echo "<script>window.location.href=\"../fiche_activite.php?action=edit&index=" . $index . "&table=fiche_activite\"</script>";
} else if (isset ( $_POST ['submitprinc'] ) and $_POST ['submitprinc'] == 'Ok') {
	$_POST = array ();
	echo "<script>window.location.href=\"./../../start.php\"</script>";
} else {
	$_POST = array ();
	echo "<script>window.location.href=\"./../../start.php\"</script>";
}

?>

